package com.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Admin;
import com.beans.Company;
import com.beans.Msg;
import com.beans.MsgReply;
import com.beans.User;
import com.service.MsgManager;
import com.util.Const;
import com.util.DateUtil;
import com.vo.Page;

public class MsgController extends UniversalController {
	private MsgManager msgManager;

	public MsgManager getMsgManager() {
		return msgManager;
	}

	public void setMsgManager(MsgManager msgManager) {
		this.msgManager = msgManager;
	}

	public ModelAndView getDelete(HttpServletRequest request,
			HttpServletResponse response) {
		int id = 0;
		Admin a=(Admin)request.getSession().getAttribute(Const.ADMIN);
		Company c=(Company)request.getSession().getAttribute(Const.COMPANY);
		User u=(User)request.getSession().getAttribute(Const.USER);
		try {
			id = Integer.parseInt(request.getParameter("id"));// ;id=12
			Msg m = (Msg) getUniversalManager().findByID(com.beans.Msg.class,id);
			if(c!=null&&m.getCompanyID()==c.getId())
				getUniversalManager().delete(m);
			else if(u!=null&&m.getUserID()==u.getId())
				getUniversalManager().delete(m);
			else if(a!=null)
				getUniversalManager().delete(m);
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			getMessage(response, "{\"success\":false,\"err\":\""+e.getMessage()+"\"}");
		}
		return null;
	}

	public ModelAndView getShowMsgList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Msg msg = new Msg();
		bind(request, msg);
		Page page = msgManager.getMsgDao().list(this.getPageNo(request),
				pageSize, msg);
		result.put("o", page);
		if (msg.getUserID() > 0)
			return new ModelAndView("user/myMsgList", result);
		else if (msg.getCompanyID() > 0)
			return new ModelAndView("company/myMsgList", result);
		else
			return new ModelAndView("msg/ShowMsgList", result);
	}

	public ModelAndView topMsgList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String top = request.getParameter("top");
		String type = request.getParameter("type");
		int top_int = 10;
		if (top != null && !"".equals(top))
			top_int = Integer.parseInt(top);
		List list = null;
		if ("new".equals(type))
			list = msgManager.getMsgDao().topNewMsg(top_int);
		else if ("view".equals(type))
			list = msgManager.getMsgDao().topViewMsg(top_int);
		else if ("fix".equals(type))
			list = msgManager.getMsgDao().topFixMsg(top_int);
		result.put("o", list);
		return new ModelAndView("msg/topMsgList", result);
	}

	public ModelAndView myMsgList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		Map result = new HashMap();
		Msg msg = new Msg();
		bind(request, msg);
		if (u != null) {
			msg.setUserID(u.getId());
			Page page = msgManager.getMsgDao().list(this.getPageNo(request),
					pageSize, msg);
			result.put("o", page);
			return new ModelAndView("user/myMsgList", result);
		} else if (c != null) {
			msg.setCompanyID(c.getId());
			Page page = msgManager.getMsgDao().list(this.getPageNo(request),
					pageSize, msg);
			result.put("o", page);
			return new ModelAndView("company/myMsgList", result);
		}
		return null;
	}

	public ModelAndView msgForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Msg msg = new Msg();
		String id=request.getParameter("id");
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		if(id==null){
			Date d=new Date();
			d.setMonth(d.getMonth()+1);
			msg.setEnddate(d);}
		else
			msg=(Msg)getUniversalManager().findByID(Msg.class, Integer.parseInt(id));
		if (u != null && c == null) {
			msg.setUserID(u.getId());
			msg.setUserName(u.getRealName());
			result.put("o",msg);
			return new ModelAndView("msg/msgForm", result);
		}
		if (c != null) {
			msg.setCompanyID(c.getId());
			msg.setCompanyName(c.getcName());
			result.put("o",msg);
			return new ModelAndView("msg/msgForm", result);
		}
		return new ModelAndView("login", result);
	}

	public ModelAndView getAddMsgReply(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		String hidden = request.getParameter("hidden");
		String hiddenName = request.getParameter("hiddenName");
		String code = (String) request.getParameter("code");
		String validateCode = (String) request.getSession().getAttribute(
				"validateCode");
		if (validateCode == null) {
			getMessage(response, "验证码过期！");
			return null;
		}
		if (null == code || "".equals(code)
				|| !code.toUpperCase().equals(validateCode.toUpperCase())) {
			getMessage(response, "验证码错误！");
			return null;
		}
		MsgReply m = new MsgReply();
		bind(request, m);
		if (m.getContent() == null || "".equals(m.getContent())) {
			getMessage(response, "请输入留言内容！");
			return null;
		}
		if (u != null) {
			m.setUserID(u.getId());
			if ("Y".equals(hidden))
				m.setUserName(hiddenName + "(匿名)");
			else
				m.setUserName(u.getRealName());
			getUniversalManager().insert(m);
			getMessage(response, "留言成功！");
			return null;
		} else if (c != null) {
			m.setCompanyID(c.getId());
			if ("Y".equals(hidden))
				m.setCompanyName(hiddenName + "(匿名)");
			else
				m.setCompanyName(c.getcName());
			getUniversalManager().insert(m);
			getMessage(response, "留言成功！");
			return null;
		}
		getMessage(response, "您还没有登录！请登录后发表留言。");
		return null;
	}

	public ModelAndView getShowMsg(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		String id = request.getParameter("id");
		try {
			Msg msg = (Msg) getUniversalManager().findByID(Msg.class,
					Integer.parseInt(id));
			msgManager.getMsgDao().addViews(msg.getId());
			result.put("o", msg);
			return new ModelAndView("msg/showMsg", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ModelAndView getInsert(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Msg msg = new Msg();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		String enddate=request.getParameter("enddate2");
		try {
			msg.setEnddate(DateUtil.convertStringToDate(enddate));
			
		} catch (ParseException e1) {
			e1.printStackTrace();
			msg.setEnddate(new Date());
		}
		try {
			bind(request, msg);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		msg.setInputdate(new Date());
		Date d = new Date();
		if (d.getMonth() < 11)
			d.setMonth(d.getMonth() + 1);
		else
			d.setYear(d.getYear() + 1);
		if (msg.getEnddate() == null)
			msg.setEnddate(d);
		try {
			if (u != null && c == null) {
				msg.setUserID(u.getId());
				msg.setUserName(u.getRealName());
				getUniversalManager().insert(msg);
				getMessage(response, "{\"success\":true,\"err\":\"\"}");
			}

			if (c != null) {
				msg.setCompanyID(c.getId());
				msg.setCompanyName(c.getcName());
				getUniversalManager().insert(msg);
				getMessage(response, "{\"success\":true,\"err\":\"\"}");
			}
		} catch (Exception e) {
			getMessage(response, "{\"success\":false,\"err\":\""
					+ e.getMessage() + "\"}");
		}
		return null;
	}
	public ModelAndView getUpdate(HttpServletRequest request,
			HttpServletResponse response){
		Map result = new HashMap();
		Msg msg = new Msg();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		String enddate=request.getParameter("enddate2");
		try {
			msg.setEnddate(DateUtil.convertStringToDate(enddate));
			
		} catch (ParseException e1) {
			e1.printStackTrace();
			msg.setEnddate(new Date());
		}
		try {
			bind(request, msg);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		msg.setInputdate(new Date());
		Date d = new Date();
		if (d.getMonth() < 11)
			d.setMonth(d.getMonth() + 1);
		else
			d.setYear(d.getYear() + 1);
		if (msg.getEnddate() == null)
			msg.setEnddate(d);
		try {
			if (u != null && c == null) {
				msg.setUserID(u.getId());
				msg.setUserName(u.getRealName());
				getUniversalManager().update(msg);
				getMessage(response, "{\"success\":true,\"err\":\"\"}");
			}

			if (c != null) {
				msg.setCompanyID(c.getId());
				msg.setCompanyName(c.getcName());
				getUniversalManager().update(msg);
				getMessage(response, "{\"success\":true,\"err\":\"\"}");
			}
		} catch (Exception e) {
			getMessage(response, "{\"success\":false,\"err\":\""
					+ e.getMessage() + "\"}");
		}
		return null;
	}
	public ModelAndView msgReplyList(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Map param = new HashMap();
		String pid = request.getParameter("pid");
		String tableName = request.getParameter("tableName");
		try {
			param.put("tableName", tableName);
			param.put("pid", Integer.parseInt(pid));
			Page page = getUniversalManager().findPageByCriteria(
					this.getPageNo(request), pageSize, param, MsgReply.class);
			result.put("o", page);
			return new ModelAndView("msg/msgReplyList", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ModelAndView getFixMsg(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String fix = request.getParameter("fix");
		try {
			if (fix == null || "".equals(fix))
				msgManager.getMsgDao().fix(Integer.parseInt(id), 1);
			else
				msgManager.getMsgDao().fix(Integer.parseInt(id),
						Integer.parseInt(fix));
			getMessage(response, "{\"success\":true,\"err\":\"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			getMessage(response, "{\"success\":false,\"err\":\""
					+ e.getMessage() + "\"}");
		}
		return null;
	}
}
